
#include "cx_timer.h"
#include "vmsys.h"
#include "vmlog.h"
#include "vmio.h"

#define MAX_TIMER_COUNT 50
QINT timeId = -1;
QVOID *pArrFunction[MAX_TIMER_COUNT];
QVOID *pArrParam[MAX_TIMER_COUNT];

typedef QINT (*iFunctionPtr)(QINT aTimeId,QVOID* aPtr);
QVOID qtime_callback(QVOID* timenotify,QINT aTimeId,QVOID* time_param)
{
	((iFunctionPtr)(timenotify))(aTimeId,time_param);
}

QVOID qtimer_on_timer_event(VMINT timeId)
{
	if((timeId >= 0) && (timeId < MAX_TIMER_COUNT))
	{
		qtime_callback(pArrFunction[timeId],timeId,pArrParam[timeId]);
	}
}

QINT cx_timer_start(QDWORD millisec, cxTimerCallBack pfNotify, QVOID* pObject)
//QINT cx_timer_start(QDWORD millisec, QVOID* timenotify,QVOID* time_param)
{
    int nsec = millisec ;
	//timeId = vm_create_timer(millisec, (VM_TIMERPROC_T)timenotify);
	timeId = vm_create_timer(nsec, qtimer_on_timer_event);
	if((timeId >= 0) && (timeId < MAX_TIMER_COUNT))
	{
		pArrFunction[timeId] = (QVOID *)pfNotify;
		pArrParam[timeId] = (QVOID *)pObject;
	}
	//write_log_by_string("create_timer");
	//write_log(timeId);
//	g_timenotify = timenotify;

	return timeId;
}

QVOID cx_timer_stop(VMINT aTimerId)
{
	vm_delete_timer(aTimerId);
}
